package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.CategoryQo;
import com.qf.staff.common.vo.CategoryVo;
import com.qf.staff.mapper.CategoryMapper;
import com.qf.staff.pojo.Category;
import com.qf.staff.pojo.Goods;
import com.qf.staff.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;

    @Override
    public IPage<CategoryVo> list(int page, int size, CategoryQo categoryQo) {
        try {
            QueryWrapper<Category> qw = new QueryWrapper<>();
            if (categoryQo.getProductName() != null) {
                qw.eq("product_name", categoryQo.getProductName());
            }
            return PageCommonBeanUtils.copyPage(categoryMapper.selectPage(new Page<>(page, size), qw),
                    new Page<>(), CategoryVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public IPage<CategoryVo> listSub(int page, int size, CategoryQo categoryQo) {
        try {
            MPJLambdaWrapper<Category> wrapper = new MPJLambdaWrapper<Category>()
                    .selectAll(Category.class)
                    .selectAll(Goods.class)
                    .leftJoin(Goods.class, Goods::getCategoryId, Category::getId);
            return categoryMapper.selectJoinPage(new Page<>(page, size), CategoryVo.class, wrapper);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    @Override
    public int delete(CategoryQo categoryQo) {
        try {
            Category category = new Category();
            BeanUtils.copyProperties(categoryQo, category);
            return categoryMapper.updateByStatus(category);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Transactional
    @Override
    public int deleteAll(List<Integer> ids, int status) {
        try {
            int num = categoryMapper.updateByAllStatus(ids, status);
            if (num < ids.size()) {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
            return num;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public int modify(CategoryQo categoryQo) {
        try {
            Category category = new Category();
            BeanUtils.copyProperties(categoryQo, category);
            return categoryMapper.updateById(category);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(CategoryQo categoryQo) {
        try {
            Category category = new Category();
            BeanUtils.copyProperties(categoryQo, category);
            return categoryMapper.insert(category);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

}
